<!DOCTYPE HTML>
<html>

<head>
    <title>设置cookie</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
        li {
            margin-top: 20px;
        }

        .center {
            margin: 0 auto;
            width: 50%;
            max-width: 500px;
        }

        #cookieStr {
            width: 80%
        }

        .hidden {
            display: hidden;
        }
    </style>
</head>

<body>
    <div class="center">
        <div>
            <p>
                请输入cookie，形式为 <br />
                name1=value1; name2=value2; ...
            </p>
            <input type="text" id="cookieStr" name="cookieStr" value="_gid" />
            <br />
            <br />

            <input type="submit" id="resetCookie" value="修改">
            <input type="submit" id="go" value="跳转">
            </form>
        </div>
    </div>
    <script>

        document.getElementById("resetCookie").onclick = function () {
            var cookieStr = document.getElementById("cookieStr").value;
            parseCookieStr(cookieStr);
        }

        document.getElementById("go").onclick = function () {
            window.location = window.location.origin;
        }

        var current_domain = window.location.hostname;
        // var domain_parts = current_domain.split('.');
        // var domain_parts_len = domain_parts.length;
        // var root_domain = domain_parts[domain_parts_len - 2] + '.' + domain_parts[domain_parts_len - 1];
        var root_domain = current_domain;
        var dot_root_domain = '.' + root_domain;
        console.log("根域名是：", root_domain);
        var domainMap = {
            '_device_id': [root_domain, 30],
            '__Host-user_session_same_site': [root_domain, 30],
            'user_session': [root_domain, 30],
            'has_recent_activity': [root_domain, 30],
            '_ga': [dot_root_domain, 30],
            'dotcom_user': [dot_root_domain, 30],
            'logged_in': [dot_root_domain, 30],
            '_octo': [dot_root_domain, 30],
            '_gh_sess': [root_domain, 0],
            'color_mode': [dot_root_domain, 0],
            'tz': [dot_root_domain, 0],
        }
        function parseCookieStr(cookieStr) {
            var cookieList = cookieStr.split(';');
            cookieList.forEach(function (name_value) {
                var pair = name_value.trim().split('=');
                var name = pair[0].trim();
                var value = pair[1].trim();
                var domain = name in domainMap ? domainMap[name][0] : window.location.host;
                var ttl = name in domainMap ? domainMap[name][1] : 0;
                setCookie(name, value, domain, ttl)
            });
        }
        function delCookie(name) {
            console.log('删除cookie: ', name)
            document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
        }

        //清除所有cookie函数
        function delAllCookie() {
            var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
            if (keys) {
                for (var i = keys.length; i--;)
                    delCookie(keys[i]);
            }
        }
        function setCookie(name, value, domain, path = '/', ttl = 30) {
            console.log(name, domain);
            if (ttl != 0) {
                var exp = new Date();
                exp.setTime(exp.getTime() + ttl * 24 * 60 * 60 * 1000);
                document.cookie = `${name}=${value}; domain=${domain}; path=${path}; expires=${exp.toGMTString()}`;
            } else {
                document.cookie = `${name}=${value}; domain=${domain}; path=${path}`;
            }
        }

        function getCookie(cname) {
            var name = cname + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i].trim();
                if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
            }
            return "";
        }
    </script>

</body>

</html>